import Vue from 'vue'
import VueRouter from 'vue-router'
//import Home from '../views/Home.vue'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Welcome from '../components/Welcome.vue'
import Users from '../components/user/Users.vue'
import Rights from '../components/power/Rights.vue'
import Roles from '../components/power/Roles.vue'
import Cate from '../components/goods/Cate.vue'
import Params from '../components/goods/Params.vue'
import Goods from '../components/goods/Goods.vue'
import Add from '../components/goods/Add.vue'
import Orders from '../components/order/Orders.vue'
import Reports from '../components/report/Reports.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/login'
  },
  {
    path:'/login',
    name:'Login',
    component: Login
  },
  {
    path:'/home',
    name:'Home',
    component:Home,
    redirect:'/welcome',
    children:[
      {path:'/welcome', component:Welcome, name:'Welcome'},
      {path:'/users', component: Users, name:'Users'},
      {path:'/rights', component: Rights, name:'Rights'},
      {path:'/roles', component: Roles, name:'Roles'},
      {path:'/categories', component: Cate},
      {path:'/params', component: Params, name:'Params'},
      {path:'/goods', component: Goods, name:'Goods'},
      {path:'/add', component: Add, name:'Add'},
      {path:'/orders', component:Orders, name:'Orders'},
      {path:'/reports', component:Reports, name:'Reports'}
    ]

  }
  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
// 挂载路由守卫
router.beforeEach((to,from,next)=>{
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数 表示放行
  // next() 放行 next('/login') 强制跳转
  if(to.path ==="/login") return next()
  const tokenStr = window.sessionStorage.getItem('token');
  if(!tokenStr)  return next('/login')
  next()
})
export default router
